//
//  EmulatorViewController.m
//  DesignPatternDemo
//
//  Created by xiulian.yin on 16/3/10.
//  Copyright © 2016年 YIN.INC. All rights reserved.
//

#import "EmulatorViewController.h"
#import "UIView+Utility.h"

#define XMargin  50
#define YMargin  10
@interface EmulatorViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) NSMutableArray *marrayData;
@property (nonatomic, strong) UITextField *inputField;
@property (nonatomic, strong) UILabel *enumratorLabel;
@property (nonatomic, strong) UILabel *blockLabel;
@property (nonatomic, strong) UILabel *quickLabel;
@property (nonatomic, strong) UILabel *innerLabel;
@property (nonatomic, strong) UILabel *normalLabel;
@end

@implementation EmulatorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    
    _inputField = [[UITextField alloc] initWithFrame:CGRectMake(XMargin, 150, 200, 40)];
    [_inputField setDelegate:self];
    [_inputField setPlaceholder:@"输入数组容量"];
    [_inputField.layer setBorderColor:[UIColor grayColor].CGColor];
    [_inputField.layer setBorderWidth:1];
    [_inputField setTextAlignment:NSTextAlignmentCenter];
    [self.view addSubview:_inputField];
    
    UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [doneBtn setTitle:@"Test" forState:UIControlStateNormal];
    [doneBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [doneBtn.layer setBorderWidth:1];
    [doneBtn.layer setBorderColor:[UIColor grayColor].CGColor];
    [doneBtn.layer setCornerRadius:4];
    [doneBtn setFrame:CGRectMake(CGRectGetMaxX(_inputField.frame)+10, _inputField.top, 80, 40)];
    [self.view addSubview:doneBtn];
    [doneBtn addTarget:self action:@selector(generateTestArrayAndTest) forControlEvents:UIControlEventTouchUpInside];
    
    _enumratorLabel = [[UILabel alloc] initWithFrame:CGRectMake(XMargin, doneBtn.bottom+YMargin, self.view.width-XMargin, 30)];
    [_enumratorLabel setFont:[UIFont systemFontOfSize:13]];
    [self.view addSubview:_enumratorLabel];
    
    _blockLabel = [[UILabel alloc] initWithFrame:CGRectMake(XMargin, _enumratorLabel.bottom+YMargin, self.view.width-XMargin, 30)];
    [_blockLabel setFont:[UIFont systemFontOfSize:13]];
    [self.view addSubview:_blockLabel];
    
    _quickLabel = [[UILabel alloc] initWithFrame:CGRectMake(XMargin, _blockLabel.bottom+YMargin, self.view.width-XMargin, 30)];
    [_quickLabel setFont:[UIFont systemFontOfSize:13]];
    [self.view addSubview:_quickLabel];
    
    _normalLabel = [[UILabel alloc] initWithFrame:CGRectMake(XMargin, _quickLabel.bottom+YMargin, self.view.width-XMargin, 30)];
    [_normalLabel setFont:[UIFont systemFontOfSize:13]];
    [self.view addSubview:_normalLabel];
    
    _innerLabel = [[UILabel alloc] initWithFrame:CGRectMake(XMargin, _normalLabel.bottom+YMargin, self.view.width-XMargin, 30)];
    [_innerLabel setFont:[UIFont systemFontOfSize:13]];
    [self.view addSubview:_innerLabel];
    
    
    
}

-(UILabel *)commonLabel
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(XMargin, 0, 200, 30)];
    [label setFont:[UIFont systemFontOfSize:13]];
    [label setTextColor:[UIColor blackColor]];
    return label;
}
-(void)generateTestArrayAndTest
{
    _marrayData = [[NSMutableArray alloc] init];
    NSInteger count = [_inputField.text integerValue];
    if (count > 0)
    {
        for (int i=0; i<count; i++)
        {
            [_marrayData addObject:[TestObj generateRandomSelf]];
        }
    }
    
    [self enumratorMethod];
    [self blockMethod];
    [self quickMethod];
    [self normalMethod];
    [self innerMethod];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"全部枚举完成" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [alert dismissViewControllerAnimated:YES completion:nil];
    }]];
    
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)enumratorMethod
{
    // enumrator枚举
    NSDate *startDate = [NSDate date];
    NSEnumerator *enumrator = [_marrayData objectEnumerator];
    TestObj *obj;
    while (obj = [enumrator nextObject])
    {
        [obj printSelf];
    }
    NSDate *endDate = [NSDate date];
    
    NSTimeInterval timeDuration = [endDate timeIntervalSinceDate:startDate];
    [_enumratorLabel setText:[NSString stringWithFormat:@"NSEnumerator枚举耗时:%f",timeDuration]];
}

-(void)blockMethod
{
    // block枚举
    NSDate *startDate = [NSDate date];
   
    // 循环遍历
    [_marrayData enumerateObjectsUsingBlock:^(TestObj  *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj printSelf];
    }];
    
    NSDate *endDate = [NSDate date];
    
    NSTimeInterval timeDuration = [endDate timeIntervalSinceDate:startDate];
    [_blockLabel setText:[NSString stringWithFormat:@"Block枚举耗时:%f",timeDuration]];

}

-(void)quickMethod
{
    // 快速枚举
    NSDate *startDate = [NSDate date];
    // 循环遍历
    for (TestObj *obj in _marrayData)
    {
        [obj printSelf];
    }
    
    NSDate *endDate = [NSDate date];
    
    NSTimeInterval timeDuration = [endDate timeIntervalSinceDate:startDate];
    [_quickLabel setText:[NSString stringWithFormat:@"快速枚举for-in耗时:%f",timeDuration]];

}
-(void)normalMethod
{
    // 快速枚举
    NSDate *startDate = [NSDate date];
    // 循环遍历
    for (int i =0; i<[_marrayData count]; i++)
    {
        TestObj *obj = [_marrayData objectAtIndex:i];
        [obj printSelf];
    }
    
    NSDate *endDate = [NSDate date];
    
    NSTimeInterval timeDuration = [endDate timeIntervalSinceDate:startDate];
    [_normalLabel setText:[NSString stringWithFormat:@"普通for-index枚举耗时:%f",timeDuration]];

}
-(void)innerMethod
{
    // 内部枚举
    NSDate *startDate = [NSDate date];
    
    // 循环遍历
    [_marrayData makeObjectsPerformSelector:@selector(printSelf) withObject:nil];
    
    NSDate *endDate = [NSDate date];
    
    NSTimeInterval timeDuration = [endDate timeIntervalSinceDate:startDate];
    [_innerLabel setText:[NSString stringWithFormat:@"内部枚举耗时:%f",timeDuration]];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


@implementation TestObj

-(void)printSelf
{
    NSLog(@"%@-%@",_pro1,_pro2);
}

+(TestObj *)generateRandomSelf
{
    // 获取1到100之间的整数
    int value1 = (arc4random() % 100) + 1;
    int value2 = (arc4random() % 200) + 1;
    
    TestObj *obj = [[TestObj alloc] init];
    [obj setPro1:[NSNumber numberWithInt:value1]];
    [obj setPro2:[NSNumber numberWithInt:value2]];
    return obj;
    
   
}
@end